﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL;

public class article : IHttpHandler
{
    ArticleDAL AD = new ArticleDAL();
    public void ProcessRequest(HttpContext context)
    {
        string functionname = context.Request.Form["functionname"].ToString();
        switch (functionname)
        {
            //显示文章分类
            case "jhy_category_query": jhy_category_query(); break;
            //添加文章分类
            case "jhy_category_add": jhy_category_add(); break;
            //编辑文章分类
            case "jhy_category_edit": jhy_category_edit(); break;
            //删除文章分类
            case "jhy_category_delete": jhy_category_delete(); break;
            //发布文章
            case "jhy_article_post": jhy_article_post(); break;
            //修改文章
            case "jhy_article_edit": jhy_article_edit(); break;
            //删除文章
            case "jhy_article_delete": jhy_article_delete(); break;
            //文章列表
            case "jhy_article_query": jhy_article_query(); break;
            //加载要编辑的文章
            case "jhy_article_edit_load": jhy_article_edit_load(); break;
            //加载文章分了
            case "jhy_category_select_query": jhy_category_select_query(); break;


            //遍历图片附件
            case "findimg": findimg(); break;
            //删除图片附件
            case "deleteimg": deleteimg(); break;
            //首页flow
            case "jhy_index_flow": jhy_index_flow(); break;
            //details页显示文章
            case "jhy_index_details": jhy_index_details(); break;

            //列出oss附件
            case "ossjhyyeye": ossjhyyeye(); break;
            //删除oss附件
            case "ossjhyyeyedel": ossjhyyeyedel(); break;
        }
    }

    #region 显示文章分类
    public void jhy_category_query()
    {
        HttpContext.Current.Response.Write(AD.jhy_category_query());
    }
    #endregion
    #region 添加文章分类
    public void jhy_category_add()
    {
        string cate_name = HttpContext.Current.Request.Form["cate_name"].ToString();
        bool b = AD.jhy_category_add(cate_name);
        if (b)
        {
            HttpContext.Current.Response.Write("true");
        }
        else
        {
            HttpContext.Current.Response.Write("false");
        }
    }
    #endregion
    #region 编辑文章分类
    public void jhy_category_edit()
    {
        string cate_id = HttpContext.Current.Request.Form["cate_id"].ToString();
        string cate_name = HttpContext.Current.Request.Form["cate_name"].ToString();
        bool b = AD.jhy_category_edit(cate_id, cate_name);
        if (b)
        {
            HttpContext.Current.Response.Write("true");
        }
        else
        {
            HttpContext.Current.Response.Write("false");
        }
    }
    #endregion
    #region 删除文章分类
    public void jhy_category_delete()
    {
        string cate_id = HttpContext.Current.Request.Form["cate_id"].ToString();
        bool b = AD.jhy_category_delete(cate_id);
        if (b)
        {
            HttpContext.Current.Response.Write("true");
        }
        else
        {
            HttpContext.Current.Response.Write("false");
        }
    }
    #endregion


    #region 发布文章
    public void jhy_article_post()
    {
        string art_title = HttpContext.Current.Request.Form["art_title"].ToString();
        string art_intro = HttpContext.Current.Request.Form["art_intro"].ToString();
        string art_content = HttpContext.Current.Request.Form["art_content"].ToString();
        string art_state = HttpContext.Current.Request.Form["art_state"].ToString();
        string art_userid = HttpContext.Current.Request.Form["art_userid"].ToString();
        string art_cateid = HttpContext.Current.Request.Form["art_cateid"].ToString();
        string b = AD.jhy_article_post(art_title, art_intro, art_content, art_state, art_userid, art_cateid);
        if (b == "false")
        {
            HttpContext.Current.Response.Write("false");
        }
        else
        {
            HttpContext.Current.Response.Write(b);
        }
    }

    #endregion
    #region 修改文章
    public void jhy_article_edit()
    {
        string art_id = HttpContext.Current.Request.Form["art_ID"].ToString();
        string art_title = HttpContext.Current.Request.Form["art_title"].ToString();
        string art_intro = HttpContext.Current.Request.Form["art_intro"].ToString();
        string art_content = HttpContext.Current.Request.Form["art_content"].ToString();
        string art_state = HttpContext.Current.Request.Form["art_state"].ToString();
        string art_cateid = HttpContext.Current.Request.Form["art_cateid"].ToString();

        art_intro = art_intro.Replace("\"", "'");
        art_content = art_content.Replace("\"", "'");
        bool b = AD.jhy_article_edit(art_id, art_title, art_intro, art_content, art_state, art_cateid);
        if (b)
        {
            HttpContext.Current.Response.Write("true");
        }
        else
        {
            HttpContext.Current.Response.Write("false");
        }
    }
    #endregion
    #region 删除文章
    public void jhy_article_delete()
    {
        string art_id = HttpContext.Current.Request.Form["art_id"].ToString();
        bool b = AD.jhy_article_delete(art_id);
        if (b)
        {
            HttpContext.Current.Response.Write("true");
        }
        else
        {
            HttpContext.Current.Response.Write("false");
        }
    }
    #endregion
    #region 文章列表
    public void jhy_article_query()
    {
        string art_userid = HttpContext.Current.Request.Form["art_userid"].ToString();
        string art_cateid = HttpContext.Current.Request.Form["art_cateid"].ToString();
        string art_title = HttpContext.Current.Request.Form["art_title"].ToString();
        string laypage = HttpContext.Current.Request.Form["page"].ToString();
        string laylimit = HttpContext.Current.Request.Form["limit"].ToString();
        HttpContext.Current.Response.Write(AD.jhy_article_query(art_userid, art_cateid, art_title, laypage, laylimit));
    }
    #endregion
    #region 加载要编辑的文章#暂时不用。
    public void jhy_article_edit_load()
    {
        string art_id = HttpContext.Current.Request.Form["art_ID"].ToString();
        DataTable dt = new DataTable();
        dt = AD.jhy_article_edit_load(art_id);
        if (dt.Rows.Count == 0)
        {
            HttpContext.Current.Response.Write("[{\"ok\":\"false\"}]");
        }
        else
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("[{");
            sb.AppendFormat("\"art_title\":\"{0}\",", dt.Rows[0]["art_title"].ToString());
            sb.AppendFormat("\"art_intro\":\"{0}\",", dt.Rows[0]["art_intro"].ToString());
            sb.AppendFormat("\"art_content\":\"{0}\",", dt.Rows[0]["art_content"].ToString());
            sb.AppendFormat("\"art_cateid\":\"{0}\",", dt.Rows[0]["art_cateid"].ToString());
            sb.AppendFormat("\"cate_name\":\"{0}\",", dt.Rows[0]["cate_name"].ToString());
            sb.AppendFormat("\"art_visable\":\"{0}\"", dt.Rows[0]["art_visable"].ToString());
            sb.Append("}]");
            HttpContext.Current.Response.Write(sb.ToString());
        }

    }
    #endregion
    #region 加载文章分类
    public void jhy_category_select_query()
    {
        DataTable dt = new DataTable();
        dt = AD.jhy_category_select_query();
        if (dt.Rows.Count > 0)
        {
            StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in dt.Rows)
            {
                sb.AppendFormat("<option value=\"{0}\">{1}</option>", dr["cate_id"].ToString(), dr["cate_name"].ToString());
            }
            HttpContext.Current.Response.Write("<option value=\"\">选择分类</option>" + sb.ToString());
        }
        else
        {
            HttpContext.Current.Response.Write("<option value=\"\">没有分类</option>");
        }

    }
    #endregion


    #region 遍历上传的图片
    public void findimg()
    {
        string imgpath = HttpContext.Current.Server.MapPath("~/upload/ArticleImg/");
        string[] files = Directory.GetFiles(imgpath);
        string count = files.Length.ToString();
        StringBuilder Sb = new StringBuilder();
        Sb.Append("{\"code\":0,\"msg\": \"\",\"count\":" + count + ",\"data\":[");
        string filename = "";
        foreach (string file in files)
        {
            Sb.Append("{");

            if (file.LastIndexOf("\\") == -1)
            {
                filename = file.Substring(file.LastIndexOf("/") + 1);
            }
            else
            {
                filename = file.Substring(file.LastIndexOf("\\") + 1);
            }

            string fileurl = "/upload/ArticleImg/" + filename;
            string filepath = file;
            Sb.Append("\"filename\":\"" + filename + "\"");
            Sb.Append(",\"fileurl\":\"<img src='" + fileurl + "' style='width:100px;height:100px;margin:8px 0px;'>\"");
            Sb.Append(",\"filepath\":\"" + filepath.Replace("\\", "\\/") + "\"");
            Sb.Append("},");

        }
        Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1));
        Sb.Append("]}");

        HttpContext.Current.Response.Write(Sb.ToString());

    }
    #endregion
    #region 删除图片
    public void deleteimg()
    {
        var filepath = HttpContext.Current.Request.Form["filepath"].ToString();
        FileInfo file = new FileInfo(filepath);//指定文件路径
        if (file.Exists)//判断文件是否存在
        {
            file.Attributes = FileAttributes.Normal;//将文件属性设置为普通,比方说只读文件设置为普通
            file.Delete();//删除文件
            HttpContext.Current.Response.Write("true");
        }
        else
        {
            HttpContext.Current.Response.Write("false");
        }

    }
    #endregion

    #region 首页flow
    public void jhy_index_flow()
    {
        string art_title = HttpContext.Current.Request.Form["art_title"].ToString();
        string laypage = HttpContext.Current.Request.Form["laypage"].ToString();
        HttpContext.Current.Response.Write(AD.jhy_index_flow(art_title, laypage));
        //DataSet ds = new DataSet();
        //ds = AD.jhy_index_flow(art_title, laypage);
        //string totalpage = ds.Tables[1].Rows[0][0].ToString();
        //DataTable dt = new DataTable();
        //dt = ds.Tables[0];
        //if (dt.Rows.Count > 0)
        //{

        //    QXHelper QX = new QXHelper();

        //    HttpContext.Current.Response.Write("{\"data\":\"" + QX.TableToJson(dt) + "\",\"totalpage\":\"" + totalpage + "\"}");
        //}
        //else
        //{
        //    HttpContext.Current.Response.Write("false");
        //}





    }
    #endregion
    #region details页显示文章
    public void jhy_index_details()
    {
        string art_id = HttpContext.Current.Request.Form["art_id"].ToString();
        HttpContext.Current.Response.Write(AD.jhy_index_details(art_id));
    }
    #endregion

    #region 列出oss附件
    public void ossjhyyeye()
    {
        OSSHelper OSS = new OSSHelper();
        string pre = HttpContext.Current.Request.Form["pre"].ToString();
        string del = HttpContext.Current.Request.Form["del"].ToString();
        HttpContext.Current.Response.Write(OSS.ListObjects("jhyyeye", pre, del));

    }
    #endregion
    #region 删除oss附件
    public void ossjhyyeyedel()
    {
        OSSHelper OSS = new OSSHelper();
        string key = HttpContext.Current.Request.Form["key"].ToString();
        bool b = OSS.DeleteObject("jhyyeye", key);
        if (b)
        {
            HttpContext.Current.Response.Write("true");
        }
        else
        {
            HttpContext.Current.Response.Write("false");
        }
    }
    #endregion

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}